package com.sebklebes.radar.radar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import com.sebklebes.radar.R;
import com.sebklebes.radar.dialogs.DialogActivity;
import com.sebklebes.radar.dialogs.DialogTypes;
import com.sebklebes.radar.dialogs.DialogFactory;

import java.util.LinkedList;
import java.util.List;

public class Radar extends Activity {

	private Button startButton;
	private MyAdapter myAdapter;
	private ListView listv;

	private final String START = "Start service";
	private final String STOP = "Stop service";

	protected View.OnClickListener startButtonListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			if (RadarService.isRunning) {
				startButton.setText(START);
				stopRadarService();
			} else {
				startButton.setText(STOP);
				startRadarService();
			}
		}
	};

	protected View.OnClickListener radarButtonListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			if (RadarService.isRunning) {
				startButton.setText(STOP);
				addRadar();
			} else {
				Toast.makeText(Radar.this, "Please start the service first.",
						Toast.LENGTH_SHORT).show();
			}
		}
	};

	private void addRadar() {
		Context context = this;
		if (RadarService.cachedLocation != null) {
			Intent intent1 = new Intent(context, DialogActivity.class);
			intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent1.putExtra(DialogFactory.DIALOGS, DialogTypes.SEND_RADAR);
			context.startActivity(intent1);
		} else {
			Toast.makeText(context, "Location not available",
					Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);
		startButton = (Button) findViewById(R.id.startButton);
		startButton.setOnClickListener(startButtonListener);
		if (!RadarService.isRunning) {
			startButton.setText(START);
		} else {
			startButton.setText(STOP);
		}

		Button radarButton = (Button) findViewById(R.id.radarButton);
		radarButton.setOnClickListener(radarButtonListener);

		// nu schimba ordinea, mega marlanie
		List<String> settings = new LinkedList<String>();
		settings.add("Show Real Time Radars");
		settings.add("Show Known Radars");
		settings.add("Show SpeedCams");
		settings.add("Auto Start at speeds over 30 km/h");
		myAdapter = new MyAdapter(settings, this);

		listv = (ListView) findViewById(R.id.listSettings);
		listv.setAdapter(myAdapter);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (!RadarService.isRunning) {
			startButton.setText(START);
		} else {
			startButton.setText(STOP);
		}
	}

	private void startRadarService() {
		if (!RadarService.isRunning) {
			startService(new Intent(Radar.this, RadarService.class));
		}
	}

	private void stopRadarService() {
		if (RadarService.isRunning) {
			stopService(new Intent(Radar.this, RadarService.class));
		}
	}
}
